package org.piggy.core.utils;

import io.netty.channel.EventLoopGroup;
import org.asynchttpclient.*;
import java.util.concurrent.CompletableFuture;

public class AsyncHttpUtil {

	private static final class SingletonHolder {
		private static final AsyncHttpUtil INSTANCE = new AsyncHttpUtil();
	}
	
	private AsyncHttpUtil() {}
	
	public static AsyncHttpUtil getInstance() {
		return SingletonHolder.INSTANCE;
	}

	private EventLoopGroup eventLoopGroup;
	
	private AsyncHttpClient asyncHttpClient;


	public void setEventLoopGroup(EventLoopGroup eventLoopGroup) {
		this.eventLoopGroup = eventLoopGroup;
	}

	public EventLoopGroup getEventLoopGroup() {
		return eventLoopGroup;
	}

	public void setAsyncHttpClient(AsyncHttpClient asyncHttpClient) {
		this.asyncHttpClient = asyncHttpClient;
	}

	public CompletableFuture<Response> executeRequest(Request request) {
		ListenableFuture<Response> future = asyncHttpClient.executeRequest(request);
		return future.toCompletableFuture();
	}

	public CompletableFuture<Response> executeRequest(RequestBuilder requestBuilder) {
		ListenableFuture<Response> future = asyncHttpClient.executeRequest(requestBuilder);
		return future.toCompletableFuture();
	}

	public <T> CompletableFuture<T> executeRequest(RequestBuilder requestBuilder, AsyncHandler<T> asyncHandler) {
		ListenableFuture<T> future = asyncHttpClient.executeRequest(requestBuilder, asyncHandler);
		return future.toCompletableFuture();
	}

	public <T> CompletableFuture<T> executeRequest(Request request, AsyncHandler<T> asyncHandler) {
		ListenableFuture<T> future = asyncHttpClient.executeRequest(request, asyncHandler);
		return future.toCompletableFuture();
	}
	
}
